﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Catalogo.Portable.Helpers
{
    public static class AssemblyHelper
    {
        private static Assembly _SpecificAssembly;

        public static T CreateSpecificInstanceOf<T>()
            where T : class
        {
            var typeInfo = typeof(T).GetTypeInfo();
            foreach (var type in GetSpecificAssembly().ExportedTypes)
            {
                if (typeInfo.IsAssignableFrom(type.GetTypeInfo()))
                    return (T)Activator.CreateInstance(type);
            }
            return null;
        }

        private static Assembly GetSpecificAssembly()
        {
            if (_SpecificAssembly == null)
                _SpecificAssembly = Assembly.Load(new AssemblyName() { Name = "Catalogo.Portable.Specific" });
            return _SpecificAssembly;
        }
    }
}
